<?php

declare(strict_types=1);

namespace Service\Code;

use App\Model\BbcModel\SmsCode;
use Service\BaseService;

class VerifySmsCodeService extends BaseService
{
    /**
     * 
     * @param string $mobile
     * @param string $code
     * @return array
     */
    public function validateCode(string $mobile,string $code)
    {
        if (empty($mobile)) {
            return Normal::returnCode(Normal::FAIL, '手机号不能为空');
        }

        if (empty($code)) {
            return Normal::returnCode(Normal::FAIL, '短信验证码不能为空');
        }

        $where = [
            ['mobile', '=', $mobile],
            ['status', '=', 0],
            ['cd', '>=', time() - 300],
            '_order_' => 'id desc',
        ];
        $sms_code = SmsCode::instance()->info($where);

        if (empty($sms_code)) {
            return Normal::returnCode(Normal::FAIL, '请发送短信验证码');
        }

        if ($code != $sms_code['code']) {
            return Normal::returnCode(Normal::FAIL, '短信验证码错误');
        }
    
        SmsCode::instance()->edit(['id' => $sms_code['id']], ['status' => 1]);
        return Normal::returnCode(Normal::SUC, '验证通过');
    }
}
